import axios from 'axios'
// 定义一个常量
const baseUrl = 'http://127.0.0.1:8888/api/private/v1/'
// 定义默认请求路径
axios.defaults.baseURL = baseUrl
// 设置axios拦截器
axios.interceptors.request.use(function (config) {
  // 获取本地存储中的token值
  var token = localStorage.getItem('token')
  // 设置请求头
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// 登录接口
export const login = (options) => {
  return axios.post('login', options).then((res) => {
    return res.data
  })
}
// 获取首页列表数据
export const getAllUserList = (options) => {
  return axios.get('users', {params: options})
    .then((res) => {
      return res.data
    })
}
// 添加用户
export const addUser = (options) => {
  return axios.post('users', options).then((res) => {
    return res.data
  })
}
// 编辑用户
export const editUser = (options) => {
  return axios.put(`users/${options.id}`, options).then((res) => {
    return res.data
  })
}
// 修改用户状态
export const changeUserState = (options) => {
  return axios.put(`users/${options.uId}/state/${options.type}`).then((res) => {
    return res.data
  })
}
// 角色列表
export const getUserRolesList = () => {
  return axios.get('roles').then((res) => {
    return res.data
  })
}
// 删除角色
export const deleteUserById = (id) => {
  return axios.delete(`users/${id}`).then((res) => {
    return res.data
  })
}
// 分配用户角色
export const setUserRole = (options) => {
  return axios.put(`users/${options.id}}/role`, options).then((res) => {
    return res.data
  })
}
// 所有权限列表
export const getAllRights = (type) => {
  return axios.get(`rights/${type}`).then((res) => {
    return res.data
  })
}
// 删除角色指定权限
export const delroleright = (roleid, rightid) => {
  return axios.delete(`roles/${roleid}/rights/${rightid}`).then((res) => {
    return res.data
  })
}
// 为指定角色授权
export const grantroleright = (options) => {
  return axios.post(`roles/${options.roleid}/rights`, {rids: options.rids}).then((res) => {
    return res.data
  })
}
// 添加角色
export const addRole = (options) => {
  return axios.post('roles', options).then((res) => {
    return res.data
  })
}
// 左侧菜单权限
export const getmenus = () => {
  return axios.get('menus').then((res) => {
    return res.data
  })
}
// 商品数据列表 =>商品分类
export const categoriesList = (options) => {
  return axios.get('categories', {params: options}).then((res) => {
    return res.data
  })
}
// 商品数据列表=>添加分类
export const addcategories = (options) => {
  return axios.post('categories', options).then((res) => {
    return res.data
  })
}
// 商品列表数据
export const goodsList = (options) => {
  return axios.get('goods', {params: options}).then((res) => {
    return res.data
  })
}
// 添加商品
export const addGoods = (options) => {
  return axios.post('goods', options).then(res => {
    return res.data
  })
}
// 根据ID查询商品
export const getGoodsById = (id) => {
  return axios.get(`goods/${id}`).then(res => {
    return res.data
  })
}
// 编辑提交商品
export const editGoods = (options) => {
  return axios.put(`goods/${options.id}`, options).then(res => {
    return res.data
  })
}
